package czh.cocoa.ui;

import czh.cocoa.foundation.NSArray;
import czh.cocoa.foundation.NSNotificationCenter;
import czh.cocoa.foundation.NSSet;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.ui.impl.UINavigationItem;
import czh.cocoa.ui.impl.UITabBarItem;
import static czh.cocoa.ui.UIApplication.*;

public class UIViewController extends UIResponder {
	
	private UIView mView;
	
	NSString mTitle;
	
	private UINavigationItem mNavigationItem;
	
	private NSArray mToolbarItems;
	
	private UINavigationController mNavigationController;
	
	private UITabBarItem mTabBarItem;
	
	private UITabBarController mTabBarController;
	
	private UIViewController mPresentedViewController;
	
	private UIViewController mPresentingViewController;
	
	public UIViewController() {
		
	}
	
	public UIView view() {
		if (mView == null) {  
			loadView();
			viewDidLoad();
		}
		return mView;
	}
	
	public void setView(UIView view) {
		mView = view;
		view.setViewDelegate(this);
	}
	
	protected void loadView() {
		CGRect frame = UIScreen.mainScreen().applicationFrame();
		UIView view = new UIView(frame);
		setView(view);
	}
	
	protected void viewDidLoad() {
		
	}
	
	public boolean isViewLoaded() {
		return mView != null;
	}
	
	protected void viewWillAppear() {
		
	}
	
	protected void viewDidAppear() {
		
	}
	
	protected void viewWillDisappear() {
		
	}
	
	protected void viewDidDisappear() {
		
	}
	
	public void didReceiveMemoryWarning() {
		
	}
	
	public UIResponder nextResponder() {
		return mView != null ? mView.superview() : null;
	}
	
	public void setTitle(NSString title) {
		mTitle = title;
	}
	
	public NSString title() {
		return mTitle;
	}
	
	public UINavigationItem navigationItem() {
		if (mNavigationItem == null) {
			mNavigationItem = new UINavigationItem(mTitle);
		}
		return mNavigationItem;
	}
	
	public UINavigationController navigationController() {
		return mNavigationController;
	}
	
	void setNavigationController(UINavigationController navi) {
		mNavigationController = navi;
	}
	
	public void setToolbarItems(NSArray items) {
		mToolbarItems = items;
	}
	
	public NSArray toolbarItems() {
		return mToolbarItems;
	}
	
	public void setTabBarItem(UITabBarItem item) {
		mTabBarItem = item;
	}
	
	public UITabBarItem tabBarItem() {
		if (mTabBarItem == null) {
			mTabBarItem = new UITabBarItem(mTitle, null, 0);
		}
		return mTabBarItem;
	}
	
	public UITabBarController tabBarController() {
		return mTabBarController;
	}
	
	void setTabBarController(UITabBarController tab) {
		mTabBarController = tab;
	}
	
	void setPresentedViewController(UIViewController ctrl) {
		mPresentedViewController = ctrl;
	}
	
	public UIViewController presentedViewController() {
		return mPresentedViewController;
	}
	
	void setPresentingViewController(UIViewController ctrl) {
		mPresentingViewController = ctrl;
	}
	
	public UIViewController presentingViewController() {
		return mPresentingViewController;
	}
	
	public void presentModalViewControllerAnimated(UIViewController ctrl, boolean animated) {
		setPresentedViewController(ctrl);
		ctrl.setPresentingViewController(this);
		
		UIWindow window = UIApplication.sharedApplication().keyWindow();
		if (window != null) { 
			final UIView view = ctrl.view();
			final CGRect frame = window.bounds();
			
			if (animated) { 
				frame.origin.y = frame.size.height;
				view.setFrame(frame);
				window.addSubview(view);
				
				UIView.animateWithDurationAnimationsCompletion(0.3, new UIAnimationBlock() {
					@Override
					public void run() {
						frame.origin.y = 0;
						view.setFrame(frame); 
					}
				}, new UIAnimationBlock.Completion() {
					@Override
					public void run(boolean finished) {
						if (isViewLoaded()) { 
							view().removeFromSuperview();
						}
					}
				});
			} else {
				
				view.setFrame(frame);
				window.addSubview(view);
				if (isViewLoaded()) { 
					view().removeFromSuperview();
				}
				
			}
		}
		
	} 
	
	public void dismissModalViewControllerAnimated(boolean animated) {
		UIViewController ctrl = mPresentedViewController;
		
		if (ctrl == null) {
			ctrl = mPresentingViewController;
			if (ctrl != null) {
				ctrl.dismissModalViewControllerAnimated(animated);
			}
			return;
		}
		
		if (ctrl != null) {
			UIWindow window = UIApplication.sharedApplication().keyWindow();
			if (window != null) { 
				final UIView view = ctrl.view();
				final CGRect frame = window.bounds();
				
				if (isViewLoaded()) {
					window.insertSubviewBelowSubview(view(), view);
				}
				
				if (animated) { 
					
					UIView.animateWithDurationAnimationsCompletion(0.3, new UIAnimationBlock() {
						@Override
						public void run() {
							frame.origin.y = frame.size.height;
							view.setFrame(frame); 
						}
					}, new UIAnimationBlock.Completion() {
						@Override
						public void run(boolean finished) {
							frame.origin.y = 0;
							view.setFrame(frame); 
							view.removeFromSuperview();
						}
					});
					
				} else {
					
					view.removeFromSuperview();
					
				}
			}
			
			ctrl.setPresentingViewController(null);
		}
		
		mPresentedViewController = null;
	}
	
}


